package org.jeecg.modules.land.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.land.entity.TbAssetManagement;
import org.jeecg.modules.land.service.ITbAssetManagementService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 无形资产-土地使用权明细
 * @Author: jeecg-boot
 * @Date: 2025-03-27
 * @Version: V1.0
 */
@Api(tags = "无形资产-土地使用权明细")
@RestController
@RequestMapping("/assetManagement/tbAssetManagement")
@Slf4j
public class TbAssetManagementController extends JeecgController<TbAssetManagement, ITbAssetManagementService> {
    @Autowired
    private ITbAssetManagementService tbAssetManagementService;

    /**
     * 分页列表查询
     *
     * @param req
     * @return
     */
    //@AutoLog(value = "无形资产-土地使用权明细-分页列表查询")
    @ApiOperation(value = "无形资产-土地使用权明细-分页列表查询", notes = "无形资产-土地使用权明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TbAssetManagement>> queryPageList(TbAssetManagement tbAssetManagement,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        List<TbAssetManagement> statistics = tbAssetManagementService.statistics(tbAssetManagement, req.getParameterMap());
        IPage<TbAssetManagement> page = new Page(pageNo, pageSize);
        page.setTotal(statistics.size());
        int startIndex = (pageNo - 1) * pageSize; // 起始索引
        int endIndex = Math.min(startIndex + pageSize, statistics.size());
        if (startIndex >= endIndex) {
            return Result.OK(page);
        }
        page.setRecords(statistics.subList(startIndex, endIndex));
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @ApiOperation(value = "无形资产-土地使用权明细-删除全部数据", notes = "无形资产-土地使用权明细-删除全部数据")
    @DeleteMapping(value = "/removeAll")
    public Result<String> removeAll() {
        tbAssetManagementService.removeAll();
        return Result.OK("删除成功！");
    }

    /**
     * 编辑
     *
     * @param tbAssetManagement
     * @return
     */
    @AutoLog(value = "无形资产-土地使用权明细-编辑")
    @ApiOperation(value = "无形资产-土地使用权明细-编辑", notes = "无形资产-土地使用权明细-编辑")
    @RequiresPermissions("assetManagement:tb_asset_management:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody TbAssetManagement tbAssetManagement) {
        tbAssetManagementService.updateById(tbAssetManagement);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "无形资产-土地使用权明细-通过id删除")
    @ApiOperation(value = "无形资产-土地使用权明细-通过id删除", notes = "无形资产-土地使用权明细-通过id删除")
    @RequiresPermissions("assetManagement:tb_asset_management:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        tbAssetManagementService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "无形资产-土地使用权明细-批量删除")
    @ApiOperation(value = "无形资产-土地使用权明细-批量删除", notes = "无形资产-土地使用权明细-批量删除")
    @RequiresPermissions("assetManagement:tb_asset_management:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.tbAssetManagementService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "无形资产-土地使用权明细-通过id查询")
    @ApiOperation(value = "无形资产-土地使用权明细-通过id查询", notes = "无形资产-土地使用权明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<TbAssetManagement> queryById(@RequestParam(name = "id", required = true) String id) {
        TbAssetManagement tbAssetManagement = tbAssetManagementService.getById(id);
        if (tbAssetManagement == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(tbAssetManagement);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tbAssetManagement
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbAssetManagement tbAssetManagement) {
        return this.exportXls(tbAssetManagement, request, TbAssetManagement.class, "无形资产-土地使用权明细");
    }

    @Resource
    private JeecgBaseConfig jeecgBaseConfig;

    protected ModelAndView exportXls(TbAssetManagement tbAssetManagement, HttpServletRequest request, Class clazz, String title) {
        // Step.1 组装查询条件
        // Step.2 获取导出数据
        List<TbAssetManagement> exportList = tbAssetManagementService.statistics(tbAssetManagement, request.getParameterMap());
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("（2024）年度市国资委出资企业无形资产-土地使用权明细表（汇总）", "单位：平方米、元", title);
        exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }


}
